"""
This module contains the CommandSelector class
"""
from HistoryManager import HistoryManager
from AliasManager import AliasManager
import os

class CommandSelector:
    """
    This class manages the AliasManager and HistoryManager objects and selects
    a command based on a predefined implicit policy that first checks the alias
    and if no alias is defined executes the shell command for that item.
    """
    def __init__(self):
        self.historymanager=HistoryManager()
        self.aliasmanager=AliasManager()
        self.readpolicy()
        
    def readpolicy(self):
        """
        This section reads the command policy and uses it to determine the command selection behaviour
        Available policy options for choosing between history and aliases are
        USE_SHORTEST_OF_BOTH_HISTORY_AND_ALIASES
        PREFER_HISTORY
        PREFER_ALIASES
        """
        
        self.POLICY="USE_SHORTEST_OF_BOTH_HISTORY_AND_ALIASES"
        ConfigError=False
        fname = os.path.join("~", ".glassbead", "policies.py")
        fname = os.path.expanduser(fname)
        if os.path.exists(fname):
            try:
                globs={}
                execfile(fname, {}, globs)
            except Exception, v:
                ConfigError=True
        else:
            
            ConfigError=True

        if not ConfigError:
           self.POLICY=globs.get("COMMAND_SELECTION_POLICY")
           print self.POLICY

    def GetAutoCompletedCommand(self,str):
        historymatch=self.historymanager.getclosesthistoryterm(str)
        aliasmatch=self.aliasmanager.findclosestalias(str)
    
        if self.POLICY=="PREFER_ALIASES":
            if aliasmatch!="":
                return aliasmatch
            else:
                return historymatch

        if self.POLICY=="PREFER_HISTORY":
            if historymatch!="":
                return historymatch
            else:
                return aliasmatch

        if self.POLICY=="USE_SHORTEST_OF_BOTH_HISTORY_AND_ALIASES":
            hlen=len(historymatch)
            alen=len(aliasmatch)
            if alen==0:
                return historymatch
            if hlen==0:
                return aliasmatch
            if alen>hlen:
                return aliasmatch
            else:
                return historymatch

    def ExecuteCommand(self,str):
        aliascommand=self.aliasmanager.getcommandforalias(str)
        if aliascommand!="":
            aliascommand=aliascommand+'&'
            os.system(aliascommand)
            self.historymanager.appendentry(str)
        else:
            os.system(str+'&')
            self.historymanager.appendentry(str)
        
    def Save(self):
        self.historymanager.save()
        self.aliasmanager.writealiases()

    def GetSuggestionList(self,str):
        li1=self.historymanager.getclosesthistorylist(str)
        li2=self.aliasmanager.findclosestaliaslist(str)
        li2.extend(li1)
        return li2
    
if __name__=="__main__":
    c=CommandSelector()
    print c.GetAutoCompletedCommand('f')